package cn.alcatraz.basic.utils;

import cn.alcatraz.basic.constant.BaseConstant;
import cn.alcatraz.org.domain.Shop;
import cn.alcatraz.org.domain.ShopAuditLog;
import cn.alcatraz.org.mapper.ShopAuditLogMapper;
import cn.alcatraz.org.mapper.ShopMapper;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Map;
import java.util.Set;

public class EmailUtil {

    private static JavaMailSender javaMailSender = ApplicationContextGetBeanHelper.getBean(JavaMailSender.class);

    public static void sendMail(String Title, String text, String emailSetTo, Map<String,String> filePaths, boolean isHtml){
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //设置发送人
            helper.setFrom(BaseConstant.EMAIL_FROM);
            helper.setSubject(Title);
            //设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText(text,isHtml);
            //添加附件
            if (filePaths != null) {
                Set<String> keySet = filePaths.keySet();
                for (String s : keySet) {
                    helper.addAttachment(s,new File(filePaths.get(s)));
                }
            }
            //设置收件人
            helper.setTo(emailSetTo);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
